package com.room.search.web.rest;

import com.room.search.constant.Constants;
import com.room.search.core.page.TableDataInfo;
import com.room.search.core.redis.RedisCache;
import com.room.search.domain.model.LoginUser;
import com.room.search.response.RoomResult;
import com.room.search.service.RoomUserOnlineService;
import com.room.search.service.dto.RoomUserOnlineDTO;
import com.room.search.service.mapper.RoomOnlineUserMapper;
import com.room.search.util.PageUtils;
import com.room.search.util.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * By   : ghd.bright
 * Time : 2022/3/12
 * Description: 系统资源监控控制器
 */

@RestController
@RequestMapping("/api")
public class RoomOnlineResource {

    private final RedisCache redisCache;
    private final RoomOnlineUserMapper roomOnlineUserMapper;

    public RoomOnlineResource(RedisCache redisCache, RoomOnlineUserMapper roomOnlineUserMapper) {
        this.redisCache = redisCache;
        this.roomOnlineUserMapper = roomOnlineUserMapper;
    }

    @PreAuthorize("@ss.hasPermi('monitor:online:list')")
    @GetMapping("/online")
    public TableDataInfo online(String ipaddr, String userName)
    {
        Collection<String> keys = redisCache.keys(Constants.LOGIN_USER_KEY + "*");
        List<RoomUserOnlineDTO> userOnlineList = new ArrayList<>();
        for (String key : keys)
        {
            LoginUser user = redisCache.getCacheObject(key);
            if (StringUtils.isNotEmpty(ipaddr) && StringUtils.isNotEmpty(userName))
            {
                if (StringUtils.equals(ipaddr, user.getIpaddr()) && StringUtils.equals(userName, user.getUsername()))
                {
                    userOnlineList.add(roomOnlineUserMapper.toOnlineDto(user));
                }
            }
            else if (StringUtils.isNotEmpty(ipaddr))
            {
                if (StringUtils.equals(ipaddr, user.getIpaddr()))
                {
                    userOnlineList.add(roomOnlineUserMapper.toOnlineDto(user));
                }
            }
            else if (StringUtils.isNotEmpty(userName) && StringUtils.isNotNull(user.getRoomUser()))
            {
                if (StringUtils.equals(userName, user.getUsername()))
                {
                    userOnlineList.add(roomOnlineUserMapper.toOnlineDto(user));
                }
            }
            else
            {
                userOnlineList.add(roomOnlineUserMapper.toOnlineDto(user));
            }
        }
        Collections.reverse(userOnlineList);
        userOnlineList.removeAll(Collections.singleton(null));
        Pageable of = PageRequest.of(0, 7);
        Page<RoomUserOnlineDTO> page = PageUtils.listConvertToPage1(userOnlineList, of);
        return TableDataInfo.getTableDataInfo(page);
    }


    /**
     * 强退用户
     */
    @PreAuthorize("@ss.hasPermi('monitor:online:forceLogout')")
    @DeleteMapping("/{username}")
    public RoomResult forceLogout(@PathVariable String username)
    {
        redisCache.deleteObject(Constants.LOGIN_TOKEN_KEY + username);
        return RoomResult.success();
    }


}
